<template>
  <div>
    <transition name="my-modal-fade">
      <div class="my-modal__wrapper" @click.self="handleClose" v-show="show">
        <div class="my-modal" :class="[ 'my-modal--' + size, 'my-modal--' + position ]">
          <div class="my-modal__header">
            <div v-if="$slots.header">
              <slot name="header" />
            </div>
            <span v-else class="my-modal__title">{{title}}</span>

            <span class="my-modal__header-btn" v-if="showClose" @click="handleClose">
              <i class="my-icon-close"></i>
            </span>
          </div>
          <div class="my-modal__content">
            <slot />
          </div>
          <div class="my-modal__footer">
            <slot name="footer" />
          </div>
        </div>
      </div>
    </transition>
    <transition name="my-modal__mask-fade" v-if="mask">
      <div class="my-modal__mask" v-show="show"></div>
    </transition>
  </div>
</template>

<script>
export default {
  name: "MyModal",
  props: {
    show: Boolean,
    title: {
      type: String,
      default: "",
    },
    size: {
      type: String,
      default: "md",
    },
    position: {
      type: String,
      default: "top",
    },
    mask: {
      type: Boolean,
      default: true,
    },
    showClose: {
      type: Boolean,
      default: true,
    },
  },
  methods: {
    handleClose() {
      this.$emit("update:show", false);
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss">
.my-modal__wrapper {
  z-index: 1000;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  transition: 0.3s ease-out;
}

.my-modal__mask {
  z-index: 999;
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba($color: #000000, $alpha: 0.3);
  transition: 0.15s linear;
}

.my-modal {
  position: relative;
  max-width: 800px;
  max-height: calc(100vh - 80px);
  margin: 0 auto;
  border-radius: 12px;
  background-color: #ffffff;
  box-shadow: 0 6px 14px rgb(0 0 0 / 10%);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  &.my-modal--top {
    transform: translateY(40px);
  }

  &.my-modal--center {
    top: 50%;
    transform: translateY(-50%);
  }

  &.my-modal--md {
    max-width: 500px;
  }

  &.my-modal--lg {
    max-width: 800px;
  }

  &.my-modal--xl {
    max-width: 1100px;
  }

  @media (max-width: 540px) {
    & {
      margin: 0 20px;
    }

    &.my-modal--md {
      margin: 0 20px;
    }
  }

  @media (max-width: 840px) {
    &.my-modal--lg {
      margin: 0 20px;
    }
  }

  @media (max-width: 1140px) {
    &.my-modal--xl {
      margin: 0 20px;
    }
  }
}

.my-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px;
  border-bottom: 1px solid #eff2f5;

  .my-modal__title {
    color: #181c32;
    font-weight: bold;
    font-size: 15px;
  }

  .my-modal__header-btn {
    width: 34px;
    height: 34px;
    line-height: 34px;
    text-align: center;
    border-radius: 50%;
    background-color: #ffffff;
    color: #a1a5b7;
    transition: 0.3s;
    cursor: pointer;

    &:hover {
      background-color: #f1faff;
      color: #00b2ff;
    }

    i {
      font-size: 13px;
    }
  }
}

.my-modal__content {
  padding: 24px;
  position: relative;
  overflow-y: auto;
  flex: 1 1 auto;
}

.my-modal__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  border-top: 1px solid #eff2f5;
  padding: 24px;
  position: relative;
}

.my-modal-fade-enter,
.my-modal-fade-leave-active {
  opacity: 0;
  transform: translate(0, -50px);
}
.my-modal__mask-fade-enter,
.my-modal__mask-fade-leave-active {
  opacity: 0;
}
</style>